const { Products } = require('../../schemas')
const mongoose = require('mongoose')
const Snowflake = require('../../utils/snowflake')

const snowflake = new Snowflake(1, 1)
const { ObjectId } = mongoose.Types

// /**
//  * 后台产品 添加 更新 查询 删除
//  * @returns
//  */

// const handleResponse = (
//   reply,
//   statusCode,
//   message,
//   data = null,
//   error = null
// ) => {
//   return reply.code(statusCode).send({
//     code: statusCode,
//     message,
//     data,
//     error,
//   })
// }

const productUpdated = async (request, reply) => {
  const { productId } = request.body
  try {
    if (!productId) {
      return reply.fail({ code: 404, msg: '更新失败！' })
    } else {
      const result = await Products.updateOne(
        { productId: productId },
        { $set: request.body }
      )
      if (result.modifiedCount > 0 && result.matchedCount > 0) {
        return reply.success({ mag: '更新成功！' })
      } else {
        return reply.fail({ code: 404, msg: '更新失败11！' })
      }
    }
  } catch (error) {
    return reply.fail({
      code: 500,
      msg: '服务器错误',
      errorMsg: error.message,
    })
  }
}

const productList = async (request, reply) => {
  const { currentPage = 1, pageSize = 10 } = request.query
  try {
    const productData = await Products.find({}, { __v: 0, _id: 0 }).lean()
    return reply.success({
      code: 200,
      msg: '查询成功！',
      data: productData,
      currentPage: Number(currentPage),
      pageSize: Number(pageSize),
    })
  } catch (error) {
    return reply.fail({ code: 500, msg: '服务器错误', error: error.message })
  }
}

// const productDeleted = async (request, reply) => {
//   const { id } = request.query
//   try {
//     const result = await Product.deleteOne({ _id: id })
//     if (result.deletedCount > 0) {
//       return handleResponse(reply, 200, '删除成功！', '删除成功！')
//     } else {
//       return handleResponse(reply, 400, '删除失败！', '删除失败！')
//     }
//   } catch (error) {
//     return handleResponse(reply, 500, '服务器错误', null, error.message)
//   }
// }

// const productDetails = async (request, reply) => {
//   const { id } = request.query
//   try {
//     const result = await Products.aggregate([
//       {
//         $match: { _id: new ObjectId(id) },
//       },
//       {
//         $addFields: {
//           id: '$_id',
//         },
//       },
//       {
//         $unset: ['__v', '_id'],
//       },
//     ])
//     const filter = result.find((item) => item.id.equals(id))
//     return handleResponse(reply, 200, '', filter)
//   } catch (error) {
//     return handleResponse(reply, 500, '服务器错误', null, error.message)
//   }
// }

const productCreated = async (request, reply) => {
  try {
    const result = new Products({
      ...request.body,
      productId: snowflake.generateUserId(13),
    })
    await result.save()
    return reply.success({
      code: 200,
      msg: '添加成功！',
      data: null,
    })
  } catch (error) {
    return reply.fail({
      code: 500,
      msg: '服务器错误',
      data: null,
      error: error.message,
    })
  }
}

module.exports = {
  productCreated,
  productUpdated,
  // productDeleted,
  productList,
  // productDetails,
}
